package com.ruoyi.common.core.domain;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@ApiModel("返回数据")
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    public static final int CODE_SUCC = 0;
    public static final int CODE_FAIL = 500;

    @ApiModelProperty(value = "返回代码。0=成功,301=警告,500=错误。其他：查看接口具体约定")
    int code;

    @ApiModelProperty("返回信息")
    String msg;

    @ApiModelProperty("返回数据")
    T data;

    public static <T> Result<T> success(String msg, T data) {
        return new Result<T>().setCode(CODE_SUCC).setMsg(msg).setData(data);
    }

    public static <T> Result<T> success(String msg) {
        return success(msg, null);
    }

    public static <T> Result<T> success() {
        return success("操作成功");
    }

    public static <T> Result<T> fail(int code, String msg, T data) {
        return new Result<T>().setCode(code).setMsg(msg).setData(data);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return fail(code, msg, null);
    }

    public static <T> Result<T> fail(String msg) {
        return fail(CODE_FAIL, msg);
    }

}
